
package Server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

 /*
  * @author Cristina
  */
public class Server implements Runnable{

    private ServerSocket server = null;
    private int port = 8005;
    private Thread thread;
    private Socket clientSocket;
    private ClientThread client;

    public Server()
    {
        Connect();
        thread = new Thread(this);
        thread.start();
    }
    public Server(int port)
    {
        this.port = port;
        Connect();
    }
    private void Connect()
    {
        try {
            server = new ServerSocket(port);
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   public void run()
   {
       int i = 0;
       while (thread != null)
       {
          try
          {
            System.out.println("Waiting for a client ..."+i);
            i++;
            //addThread(server.accept());
            clientSocket = server.accept();
            client = new ClientThread(clientSocket);
            System.out.println("New Client: "+client);
            client.start();

         }
         catch(IOException ie)
         {  System.out.println("Acceptance Error: " + ie); }
      }
   }

   public static void main(String args[])
   {
       Server server = null;
        server = new Server();


   }

}

